﻿//Cac ham xu ly tren string	
string_replace = function (haystack, needle, replacement) {
	if (needle != replacement) {
		var position = 0;
		var temp_position = 0;
		while (haystack.indexOf(needle, position) != -1) {
			temp_position = haystack.indexOf(needle, position)+(replacement.length-1);
			haystack = haystack.substr(0, haystack.indexOf(needle, position))+replacement+haystack.substr(haystack.indexOf(needle, position)+needle.length);
			position = temp_position;
		}
	}
	return haystack;
};

function string_pos(haystack :String, needle:String, start_position:Number):Number {
	tart_position = (start_position == null) ? 0 : start_position;
	return (haystack.indexOf(needle, start_position) != -1) ? haystack.indexOf(needle, start_position) : false;
};
//trace(string_pos("Hey how are you doing?","how"));
//ex: string_pos("Hey how are you doing?","How",0) RETURNS false

function string_to_array(string:String,separator:String):Array
{
	var my_string_pos:Number=0;
	var old_pos:Number=1;
	var i:Number=0;
	var my_array =Array();
	//trace("string="+string+" and string_length:"+length(string));
//	while(my_string_pos<length(string)&&old_pos<>false&&i<10)
	while(my_string_pos<length(string)&&i<10)
	{					
		old_pos = my_string_pos;
		my_string_pos = string_pos(string,separator,my_string_pos);		
		//trace("my_string_pos="+my_string_pos);
		if(my_string_pos!=false)
		{
			my_array[i]= string.substr(old_pos, my_string_pos-old_pos);
		}
		if(my_string_pos==false && old_pos>0)
		{
			my_array[i]= string.substr(old_pos, length(string)-old_pos);			
		}
		if(my_string_pos==false)
			break;
		my_string_pos ++;
		i++;
	}
	if(i==0)
		my_array[0] = string;
		return my_array;
}
